<template>
  <d2-container>
    <div class="flex-1 mt-2 ml-2">
      <div class="row-ac pb-2">
        <div><el-button type="warning" @click="addApplication" icon="el-icon-plus">添加应用</el-button></div>
      </div>
      <div class="flex-1">
        <el-table :data="softwareList" border stripe>
          <el-table-column align="center" label="序号" type="index" width="45" />
          <el-table-column align="center" label="图片" prop="image" width="80">
            <template slot-scope="scope">
              <el-popover placement="right" trigger="hover">
                <el-image slot="reference" style="width: 40px; height: 40px" :src="scope.row.cover_img" fit="cover" />
                <el-image style="width: 220px; height: 220px" :src="scope.row.cover_img" fit="contain" />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column align="center" label="应用名称" prop="title" width="130" />
          <el-table-column align="center" label="应用分类" prop="category_name" width="70" />
          <el-table-column align="center" label="版本" prop="version" width="65" />
          <el-table-column align="center" label="下载量" prop="download_count" width="50" />
          <el-table-column align="center" label="应用价格" prop="price" width="60" />
          <el-table-column align="center" label="是否免费" prop="is_free" width="60">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.is_free == 0" type="danger">收费</el-tag>
              <el-tag v-if="scope.row.is_free == 1" type="danger">免费</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="120">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
              <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex-1 ml-2 mt-2">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="queryInfo.page" layout="total, prev, pager, next" :total="softwareTotal">
        </el-pagination>
      </div>
    </div>
  </d2-container>
</template>

<script>
  import {
    softwareListUrl,
    softwareDeleteUrl
  } from "@/utils/application"
  export default {
    data() {
      return {
        queryInfo: {
          page: 1,
          page_size: 10
        },
        softwareList: [],
        softwareTotal: 1
      }
    },
    created() {
      this.getApplicationList()
    },
    methods: {
      getApplicationList() {
        this.$get(softwareListUrl, this.queryInfo).then(res => {
          if (res.data.code == 200) {
            this.softwareList = res.data.data.list
            this.softwareTotal = res.data.data.total
          }
        })
      },
      // 分页页码显示条数
      handleSizeChange(newSize) {
        // 把每页显示多少数据重新赋值
        this.queryInfo.page_size = newSize
        this.getApplicationList()
      },
      // 翻页事件
      handleCurrentChange(newPage) {
        this.queryInfo.page = newPage
        this.getApplicationList()
      },
      addApplication(){
        this.$router.push({
          path: '/application/addApplication',
        })
      },
      handleEdit(appId) {
        this.$router.push({
          path: '/application/addApplication',
          query: {
            appId: appId
          }
        })
      },
      openDelete(appId) {
        this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$del(softwareDeleteUrl + appId + '/').then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getApplicationList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>

<style>
</style>
